home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / CHESS / TEXTURE.C < prev   
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.3 KB  |  204 lines

  1. /*
  2.  * chess.c - part of the chess demo in the glut distribution.
  3.  *
  4.  * (C) Henk Kok (kok@wins.uva.nl)
  5.  *
  6.  * This file can be freely copied, changed, redistributed, etc. as long as
  7.  * this copyright notice stays intact.
  8.  */
  9.  
  10. /*
  11.  * Marble texture - shamelessly ripped from siggraph92_C23.shar
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <GL/glut.h>
  18. #include "chess.h"
  19.  
  20. #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
  21.  
  22. #define B 256
  23.  
  24. static int p[B + B + 2];
  25. static GLfloat g[B + B + 2][3];
  26. static int start = 1;
  27.  
  28. #define setup(i,b0,b1,r0,r1) \
  29.         t = vec[i] + 10000.; \
  30.         b0 = ((int)t) & (B-1); \
  31.         b1 = (b0+1) & (B-1); \
  32.         r0 = t - (int)t; \
  33.         r1 = r0 - 1.;
  34.  
  35. GLfloat noise3(GLfloat vec[3])
  36. {
  37.         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
  38.         GLfloat rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
  39.         register int i, j;
  40.  
  41.         if (start) {
  42.                 start = 0;
  43.                 init();
  44.         }
  45.  
  46.         setup(0, bx0,bx1, rx0,rx1);
  47.         setup(1, by0,by1, ry0,ry1);
  48.         setup(2, bz0,bz1, rz0,rz1);
  49.  
  50.         i = p[ bx0 ];
  51.         j = p[ bx1 ];
  52.  
  53.         b00 = p[ i + by0 ];
  54.         b10 = p[ j + by0 ];
  55.         b01 = p[ i + by1 ];
  56.         b11 = p[ j + by1 ];
  57. #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
  58.  
  59. #define surve(t) ( t * t * (3. - 2. * t) )
  60.  
  61. #define lerp(t, a, b) ( a + t * (b - a) )
  62.  
  63.         sx = surve(rx0);
  64.         sy = surve(ry0);
  65.         sz = surve(rz0);
  66.  
  67.  
  68.         q = g[ b00 + bz0 ] ; u = at(rx0,ry0,rz0);
  69.         q = g[ b10 + bz0 ] ; v = at(rx1,ry0,rz0);
  70.         a = lerp(sx, u, v);
  71.  
  72.         q = g[ b01 + bz0 ] ; u = at(rx0,ry1,rz0);
  73.         q = g[ b11 + bz0 ] ; v = at(rx1,ry1,rz0);
  74.         b = lerp(sx, u, v);
  75.  
  76.         c = lerp(sy, a, b);          /* interpolate in y at lo x */
  77.  
  78.         q = g[ b00 + bz1 ] ; u = at(rx0,ry0,rz1);
  79.         q = g[ b10 + bz1 ] ; v = at(rx1,ry0,rz1);
  80.         a = lerp(sx, u, v);
  81.  
  82.         q = g[ b01 + bz1 ] ; u = at(rx0,ry1,rz1);
  83.         q = g[ b11 + bz1 ] ; v = at(rx1,ry1,rz1);
  84.         b = lerp(sx, u, v);
  85.  
  86.         d = lerp(sy, a, b);          /* interpolate in y at hi x */
  87.  
  88.         return 1.5 * lerp(sz, c, d); /* interpolate in z */
  89. }
  90.  
  91. void
  92. init(void)
  93. {
  94.     int i, j, k;
  95.     GLfloat v[3], s;
  96.  
  97. /* Create an array of random gradient vectors uniformly on the unit sphere */
  98.  
  99.     srand(1);
  100.     for (i = 0 ; i < B ; i++) {
  101.     do {        /* Choose uniformly in a cube */
  102.         for (j=0 ; j<3 ; j++)
  103.         v[j] = (GLfloat)((rand() % (B + B)) - B) / B;
  104.         s = DOT(v,v);
  105.     } while (s > 1.0);      /* If not in sphere try again */
  106.     s = sqrt(s);
  107.     for (j = 0 ; j < 3 ; j++)       /* Else normalize */
  108.         g[i][j] = v[j] / s;
  109.     }
  110.  
  111. /* Create a pseudorandom permutation of [1..B] */
  112.  
  113.     for (i = 0 ; i < B ; i++)
  114.     p[i] = i;
  115.     for (i = B ; i > 0 ; i -= 2) {
  116.     k = p[i];
  117.     p[i] = p[j = rand() % B];
  118.     p[j] = k;
  119.     }
  120.  
  121. /* Extend g and p arrays to allow for faster indexing */
  122.  
  123.     for (i = 0 ; i < B + 2 ; i++) {
  124.     p[B + i] = p[i];
  125.     for (j = 0 ; j < 3 ; j++)
  126.         g[B + i][j] = g[i][j];
  127.     }
  128. }
  129.  
  130. GLfloat turbulence(GLfloat x, GLfloat y, GLfloat z, GLfloat lofreq, GLfloat hifreq)
  131. {
  132.     GLfloat freq, t, p[3];
  133.  
  134.     p[0] = x + 123.456;
  135.     p[1] = y;
  136.     p[2] = z;
  137.  
  138.     t = 0;
  139.     for (freq = lofreq ; freq < hifreq ; freq *= 2.) {
  140.     t += fabs(noise3(p)) / freq;
  141.     p[0] *= 2.;
  142.     p[1] *= 2.;
  143.     p[2] *= 2.;
  144.     }
  145.     return t - 0.3; /* readjust to make mean value = 0.0 */
  146. }
  147.  
  148. GLfloat marble(GLfloat x, GLfloat y, GLfloat z)
  149. {
  150.     GLfloat m;
  151.     m = turbulence(x, y, z, 0.3, 400.0);
  152.     if (m > 1.0)
  153.     m = 1.0;
  154.     if (m < 0.0)
  155.     m = 0.0;
  156.     return m;
  157. }
  158.  
  159. extern GLubyte white_square[TXSX][TXSY][3];
  160. extern GLubyte black_square[TXSX][TXSY][3];
  161. extern GLubyte wood[TXSX][TXSY][3];
  162.  
  163. void GenerateTextures(void)
  164. {
  165.     int i,j,k;
  166.     GLfloat x,y,t,w,b;
  167.     for (i=0;i<TXSX;i++)
  168.     {
  169.     for (j=0;j<TXSY;j++)
  170.     {
  171.         x = ((GLfloat) i)/20.0;
  172.         y = ((GLfloat) j)/20.0;
  173.         t = marble(x, y, 0.0);
  174.  
  175.         t = 0.2 + t;
  176.         if (t > 1.0)
  177.         t = 1.0;
  178.  
  179.         wood[i][j][0] = (0.6*t)*255;
  180.         wood[i][j][1] = (0.4*t)*255;
  181.         wood[i][j][2] = (0.5-0.4*t)*255;
  182.  
  183.         x = ((GLfloat) i)/20.0;
  184.         y = ((GLfloat) j)/20.0;
  185.         t = marble(x, y, 0.0);
  186.  
  187.         t = 0.2 + t;
  188.         if (t > 1.0)
  189.         t = 1.0;
  190.  
  191.         w = t;
  192.         b = 0.8 -t;
  193.         if (b < 0.0 )
  194.         b = 0.0;
  195.  
  196.         for (k=0;k<3;k++)
  197.         {
  198.         white_square[i][j][k] = w*255;
  199.         black_square[i][TXSY-j][k] = b*255;
  200.         }
  201.     }
  202.     }
  203. }
  204.